// 导入vue
import Vue from "vue";
// 导入vuex
import Vuex from "vuex";
// 导入data
import { list } from "../data.js";
Vue.use(Vuex);
const state = {
    list,
};
const actions = {};
const mutations = {
    allChange(state, flag) {
        state.list.forEach((item) => (item.select = flag));
    },
    delOneGood(state, id) {
        state.list = state.list.filter((item) => item.id !== id);
    },
    clearCart() {
        state.list = [];
    },
    delAllSelect() {
        state.list = state.list.filter((item) => !item.select);
    },
};
const getters = {
    allSelect() {
        return state.list.every((item) => item.select);
    },
    totalNum() {
        return state.list
            .filter((item) => item.select)
            .reduce((item, prev) => (item += prev.buy_num), 0);
    },
    totalPrice() {
        return state.list
            .filter((item) => item.select)
            .reduce((item, prev) => (item += prev.buy_num * prev.price), 0);
    },
};
// 创建一个store
const store = new Vuex.Store({
    actions,
    mutations,
    state,
    getters,
});
// 暴露store
export default store;
